<?php

namespace app\controller\Api\V1\Client;

use app\model\GameOfficialResources;
use app\request\OfficialResources;
use Exception;
use support\Request;
use support\Response;

class OfficialResourcesController
{

    /**
     * 获取下载链接
     *
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function Download(Request $request): Response
    {

        $validate = new OfficialResources;

        if (!$validate->scene('download')->check($request->all())) {
            throw new Exception($validate->getError(), 500);
        }

        $resourcesType = 1;
        if (in_array($request->input('resources_type'), [1, 2, 3])) {
            $resourcesType = $request->input('resources_type');
        }

        $info = GameOfficialResources::query()->has('TypeBind')->has('EngineBind')
            ->where('game_type', $request->input('game_type'))
            ->where('state', 1)->where('type', $resourcesType)
            ->latest('version')->first();

        if (!$info) {
            return jsonRe();
        } else {
            $info = $info->toArray();

            return jsonRe([
                'version_number' => $info['version'],
                'version_resource' => $info['url'] . '/',
            ]);
        }

    }

}